﻿Imports Windows.Networking.Sockets
Imports Windows.Networking
Imports Windows.Storage.Streams
Imports LSW.Exceptions

Namespace Extension
    Public Module SocketHelper
        Public Async Function Connect(hostname As String, port As String) As Task(Of StreamSocket)
            Dim soctet As New StreamSocket
            Await soctet.ConnectAsync(New HostName(hostname), port)
            Return soctet
        End Function

        <Extension>
        Public Async Function SendMessage(socket As StreamSocket, str As String) As Task(Of String)
            Try
                Dim writer As New DataWriter(socket.OutputStream)
                writer.WriteString(str)
                Await writer.StoreAsync
                Dim reader As New DataReader(socket.InputStream)
                reader.InputStreamOptions = InputStreamOptions.Partial
                Await reader.LoadAsync(1024)
                Return reader.ReadString(reader.UnconsumedBufferLength)
            Catch ex As Exception
                Throw New LSWFrameworkException(ex)
            End Try
        End Function
    End Module
End Namespace